package Gui;

import Current.Account;
import Current.ClassDT;
import Current.Race;
import Preferences.AccountInUse;
import info.clearthought.layout.TableLayout;
import info.clearthought.layout.TableLayoutConstraints;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * @author Fork
 */
class AccountCreation extends JFrame {
    public AccountCreation() {
        initComponents();

    }

    private void enterKeyPressed(KeyEvent e) {
        if (e.getKeyChar() == '\n')
            createButtonMouseClicked(null);
    }

    private void nameFieldKeyPressed(KeyEvent e) {
        if (e.getKeyChar() == '\n')
            createButtonMouseClicked(null);
    }

    private void levelSpinnerKeyPressed(KeyEvent e) {
        if (e.getKeyChar() == '\n') {
            createButtonMouseClicked(null);
        }
    }

    private void createButtonMouseClicked(MouseEvent e) {
        if (!nameField.getText().equals("")) {
            String actName = nameField.getText();
            Integer actLevel = (Integer) levelSpinner.getValue();
            Race actRace = (Race) raceBox.getSelectedItem();
            ClassDT actClass = (ClassDT) classDTComboBox.getSelectedItem();

            Account act = new Account(actName, actRace, actClass, actLevel);
            AccountInUse.setAccountInUse(act);

            Main.startCalc();

            this.dispose();
        } else {
        
        }
        //TODO: Throw error window
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        // Generated using JFormDesigner Open Source Project license - unknown
        JPanel panel1 = new JPanel();
        JLabel name = new JLabel();
        nameField = new JTextField();
        JLabel level = new JLabel();
        levelSpinner = new JSpinner();
        JLabel race = new JLabel();
        raceBox = new JComboBox();
        JLabel classDT = new JLabel();
        classDTComboBox = new JComboBox();
        JPanel panel2 = new JPanel();
        JButton createButton = new JButton();
        JButton loadButton = new JButton();

        //======== this ========
        setTitle("DT Calc");
        setResizable(false);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setName("this");
        addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                enterKeyPressed(e);
            }
        });
        Container contentPane = getContentPane();
        contentPane.setLayout(new TableLayout(new double[][]{
                {15, TableLayout.FILL, 15},
                {15, TableLayout.PREFERRED, TableLayout.PREFERRED, 15}}));
        ((TableLayout) contentPane.getLayout()).setHGap(5);
        ((TableLayout) contentPane.getLayout()).setVGap(5);

        //======== panel1 ========
        {
            panel1.setName("panel1");
            panel1.setLayout(new TableLayout(new double[][]{
                    {TableLayout.MINIMUM, TableLayout.FILL},
                    {TableLayout.PREFERRED, TableLayout.PREFERRED, TableLayout.PREFERRED, TableLayout.PREFERRED}}));
            ((TableLayout) panel1.getLayout()).setHGap(5);
            ((TableLayout) panel1.getLayout()).setVGap(5);

            //---- name ----
            name.setText("Name");
            name.setName("name");
            panel1.add(name, new TableLayoutConstraints(0, 0, 0, 0, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));

            //---- nameField ----
            nameField.setName("nameField");
            nameField.addKeyListener(new KeyAdapter() {
                @Override
                public void keyPressed(KeyEvent e) {
                    nameFieldKeyPressed(e);
                }
            });
            panel1.add(nameField, new TableLayoutConstraints(1, 0, 1, 0, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));

            //---- level ----
            level.setText("Level");
            level.setName("level");
            panel1.add(level, new TableLayoutConstraints(0, 1, 0, 1, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));

            //---- levelSpinner ----
            levelSpinner.setName("levelSpinner");
            levelSpinner.setModel(new SpinnerNumberModel(1, 1, 300, 1));
            levelSpinner.addKeyListener(new KeyAdapter() {
                @Override
                public void keyPressed(KeyEvent e) {
                    levelSpinnerKeyPressed(e);
                }
            });
            panel1.add(levelSpinner, new TableLayoutConstraints(1, 1, 1, 1, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));

            //---- race ----
            race.setText("Race");
            race.setName("race");
            panel1.add(race, new TableLayoutConstraints(0, 2, 0, 2, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));

            //---- raceBox ----
            raceBox.setModel(new DefaultComboBoxModel(Race.values()));
            raceBox.setMaximumRowCount(4);
            raceBox.setName("raceBox");
            panel1.add(raceBox, new TableLayoutConstraints(1, 2, 1, 2, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));

            //---- classDT ----
            classDT.setText("Class");
            classDT.setName("classDT");
            panel1.add(classDT, new TableLayoutConstraints(0, 3, 0, 3, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));

            //---- classDTComboBox ----
            classDTComboBox.setMaximumRowCount(4);
            classDTComboBox.setModel(new DefaultComboBoxModel(ClassDT.values()));
            classDTComboBox.setName("classDTComboBox");
            panel1.add(classDTComboBox, new TableLayoutConstraints(1, 3, 1, 3, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
        }
        contentPane.add(panel1, new TableLayoutConstraints(1, 1, 1, 1, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));

        //======== panel2 ========
        {
            panel2.setName("panel2");
            panel2.setLayout(new TableLayout(new double[][]{
                    {TableLayout.FILL},
                    {TableLayout.PREFERRED}}));
            ((TableLayout) panel2.getLayout()).setHGap(5);
            ((TableLayout) panel2.getLayout()).setVGap(5);

            //---- createButton ----
            createButton.setText("Create");
            createButton.setName("createButton");
            createButton.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    createButtonMouseClicked(e);
                }
            });
            panel2.add(createButton, new TableLayoutConstraints(0, 0, 0, 0, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
            
        }
        contentPane.add(panel2, new TableLayoutConstraints(1, 2, 1, 2, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
        pack();
        setLocationRelativeTo(null);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    private JTextField nameField;
    private JSpinner levelSpinner;
    private JComboBox raceBox;
    private JComboBox classDTComboBox;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
